package com.infoland.afanmall.model.xilie.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.util.CONST;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.StatusBarKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.base.util.UserKit;
import com.infoland.afanmall.base.util.layoutManager.GridSpacingItemDecoration;
import com.infoland.afanmall.base.util.okhttp.OkHttpKit;
import com.infoland.afanmall.model.shopCart.activity.ShopCartActivity;
import com.infoland.afanmall.model.xilie.adapter.XlGoodsAdapter;
import com.infoland.afanmall.model.xilie.entity.XiLieEntity;
import com.infoland.afanmall.model.youXuan.activity.GoodInfoActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author 王永吉
 * @date 2018/7/18 13:04
 * 系列集合商品
 */
public class XiLieActivity extends AppCompatActivity {
    //商品集合布局
    @BindView(R.id.xi_lie_recycler)
    RecyclerView xlGoodsRecycler;
    //返回数据
    XiLieEntity xiLieEntity;
    //商品集合
    List<XiLieEntity.GoodsListBean> goodsListBeanList;
    //内容集合
    List<XiLieEntity.GsContextBean> gsContextBeanList;
    //以后没用 TODO
    @BindView(R.id.img)
    ImageView img;
    //头部title
    @BindView(R.id.title_layout)
    LinearLayout titleLayout;
    //脚部收藏按钮
    @BindView(R.id.xi_lie_bottom_shouc)
    LinearLayout shouCangBtn;
    //脚部购物车按钮
    @BindView(R.id.xi_lie_bottom_cart)
    LinearLayout shopCarBtn;
    //脚部立即购买按钮
    @BindView(R.id.xi_lie_bottom_by)
    LinearLayout buyBtn;
    //脚部填充
    @BindView(R.id.tian_chong)
    LinearLayout tianchong;
    //收藏心图片
    @BindView(R.id.xi_lie_bottom_xin)
    ImageView xin;
    //返回按钮
    @BindView(R.id.info_back)
    ImageView backBtn;
    //购物车数量
    int userCartCount;
    //商品列表适配器
    XlGoodsAdapter adapter;

    private int getLayout() {
        return R.layout.xi_lie_activity;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ButterKnife.bind(this);
        Bundle bundle = getIntent().getExtras();
        //获取系列商品数据
        getGoodsSeries(bundle.getInt("id"));
        //获取用户购物车数量
        getUserCart();
        xlGoodsRecycler.setFocusableInTouchMode(false);
        StatusBarKit.immersiveBlackTitleText(this);
        StatusBarKit.setPaddingSmart(this, titleLayout);
        //设置脚步填充高度
        StatusBarKit.setPadding(this,tianchong);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplication(), 2);
        xlGoodsRecycler.setLayoutManager(gridLayoutManager);
        xlGoodsRecycler.addItemDecoration(new GridSpacingItemDecoration(2, SizeUtils.dp2px(10), true));
        //绑定页面按钮点击事件
        btnClick();
    }

    @Override
    protected void onDestroy() {
        ToolsKit.deleteOKHttp(this);
        super.onDestroy();
    }
    //获取系列商品数据
    private void getGoodsSeries(int id){
        JSONObject param = new JSONObject();
        param.put("id",id);
        new OkHttpKit().send(
                response200 -> {
                    LogKit.E("response200 ---->   ", "  " + response200);
                    xiLieEntity = JSONObject.parseObject(response200, XiLieEntity.class);
                    //收藏状态
                    if(xiLieEntity.isIsFavor()){
                        ToolsKit.setImgView(R.mipmap.collection_ov , xin);
                    }else{
                        ToolsKit.setImgView(R.mipmap.collection , xin);
                    }
                    goodsListBeanList = xiLieEntity.getGoodsList();
                    gsContextBeanList = xiLieEntity.getGs_context();
                    //设置最上方产品系列图片
                    ToolsKit.setImgView(xiLieEntity.getGs_image(), img);
                    initAdapter();
                },
                error401 -> {
                    LogKit.E("success401 ---->   ", "  " + error401);
                },
                error -> {
                    LogKit.E("error ---->   ", "  " + error);
                }
        ).post(CONST.GET_GOOD_SERIES + UserKit.getAccessToken(), param, this);
    }
    //获取用户购物车数量
    private void getUserCart(){
        new OkHttpKit().send(
                response200 -> {
                    LogKit.E("response200 ---->   ", "  " + response200);
                    JSONArray userCart = JSONArray.parseArray(response200);
                    if(userCart.size() == 0){
                        userCartCount = 0;
                    } else {
                        for(int i = 0; i < userCart.getJSONObject(0).getJSONArray("cartList").size(); i++){
                            JSONObject job = userCart.getJSONObject(0).getJSONArray("cartList").getJSONObject(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                            userCartCount += job.getIntValue("goods_count");
                        }
                    }
                    LogKit.E("购物车数量", String.valueOf(userCartCount));
                },
                error401 -> {
                    LogKit.E("success401 ---->   ", "  " + error401);
                },
                error -> {
                    LogKit.E("error ---->   ", "  " + error);
                }
        ).post(CONST.GET_USER_CART + UserKit.getAccessToken(), new JSONObject(), this);
    }

    //初始化适配器
    private void initAdapter(){
        adapter = new XlGoodsAdapter(R.layout.xi_lie_item, goodsListBeanList);
        clickBack();
        //添加适配器
        xlGoodsRecycler.setAdapter(adapter);
    }

    //点击事件处理
    private void clickBack(){
        //跳转商品详情页面
        adapter.setCallBack((v, position, item) -> {
            Bundle param = new Bundle();
            param.putInt("id", item.getId());
//            param.putInt();
            ToolsKit.goDh(XiLieActivity.this, GoodInfoActivity.class, param);
        });
    }
    //activity按钮点击事件处理
    private void btnClick(){
        //收藏按钮点击事件
        shouCangBtn.setOnClickListener(v -> {
            JSONObject params = new JSONObject();
            params.put("id", xiLieEntity.getId());
            //系列商品收藏类型
            params.put("type", 4);
            if(xiLieEntity.isIsFavor()){
                //取消收藏
                new OkHttpKit().send(
                        response200 -> {
                            LogKit.E("response200 ---->   ", "  " + response200);
                            ToastUtils.showShort("取消收藏成功");
                            ToolsKit.setImgView(R.mipmap.collection , xin);
                        },
                        error401 -> {
                            LogKit.E("success401 ---->   ", "  " + error401);
                            ToastUtils.showShort("不登录收藏啥");
                        },
                        error -> {
                            LogKit.E("error ---->   ", "  " + error);
                        }
                ).post(CONST.DELETE_FAVORITES + UserKit.getAccessToken(), params, this);
            } else {
                //收藏
                new OkHttpKit().send(
                        response200 -> {
                            LogKit.E("response200 ---->   ", "  " + response200);
                            ToastUtils.showShort("收藏成功");
                            ToolsKit.setImgView(R.mipmap.collection_ov , xin);
                        },
                        error401 -> {
                            LogKit.E("success401 ---->   ", "  " + error401);
                            ToastUtils.showShort("不登录收藏啥");
                        },
                        error -> {
                            LogKit.E("error ---->   ", "  " + error);
                        }
                ).post(CONST.SAVE_FAVORITES + UserKit.getAccessToken(), params, this);
            }
        });
        //购物车按钮点击事件
        shopCarBtn.setOnClickListener(v -> {
            ToolsKit.goDh(XiLieActivity.this, ShopCartActivity.class);
        });
        //立即购买点击事件 TODO
        buyBtn.setOnClickListener(v -> {

        });
        //头部返回按钮
        backBtn.setOnClickListener(v -> {
            finish();
            ToolsKit.goCloseDh(this);
        });
    }
}
